package com.juzipi.demo.singleton.type4;

public class SingletonTest4 {

    public static void main(String[] args) {
        //懒汉式2，线程安全
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        System.out.println(instance.hashCode());
        System.out.println(instance1.hashCode());
        System.out.println(instance.equals(instance1));
        System.out.println(instance == instance1);
    }

}

//懒汉式2，线程安全,同步方法
class Singleton{
    private static Singleton instance;

    private Singleton(){

    }
    //提供一个静态的公有方法，加入了同步处理的代码，解决了线程安全问题
    //即懒汉式

    public static synchronized Singleton getInstance(){
        if (instance == null){
            instance = new Singleton();
        }
        return instance;
    }

}